package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.FindMenu;
import com.lanou.dllo.findfood.util.HotSeacherClick;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */

public class HotSeachAdpter extends
        RecyclerView.Adapter<HotSeachAdpter.fragmentAdpter> {

    private List<FindMenu> m;
    private HotSeacherClick mlistener;

    public void addData(List<FindMenu> result) {
        m = result;
        notifyDataSetChanged();
    }

    public  void  setImage(HotSeacherClick listener){

        mlistener = listener;
    }

    @Override
    public fragmentAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.search_activity, null);

        return new fragmentAdpter(view);
    }

    @Override
    public void onBindViewHolder(fragmentAdpter holder, int position) {
        if (m != null) {

            FindMenu result = m.get(position);
            if (result != null) {
                holder.search_count.setText(result.getCount()+"");
                if (result.getImg() != null && result.getImg().length() > 0) {
                    String imgUrl =result.getImg();
                    String url = " http://tnfs.tngou.net/image/" + imgUrl;
                    getBitmap(url, holder.img);
                }

            }

        }
        holder.position = position;
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return m != null && m.size() > 0 ? m.size() : 0;
    }

    class fragmentAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView img;
        private TextView search_count,search_discreption,search_click;
        private int position;

        public fragmentAdpter(View itemView) {
            super(itemView);
            img = (NetworkImageView) itemView.findViewById(R.id.search_image);
            search_count = (TextView) itemView.findViewById(R.id.search_count);
            search_discreption = (TextView) itemView.findViewById(R.id.search_discreption);
            search_click = (TextView) itemView.findViewById(R.id.search_click);
            search_click.setOnClickListener(this);
            img.setOnClickListener(this);

        }

        private int count = 0;

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.search_click:
                    if (count == 0) {
                        if (m != null && m.size() > 0) {
                            FindMenu menuResult = m.get(position);
                            String str = menuResult.getDescription();
                            search_discreption.setText(str);
                        }
                        count = 1;
                    } else if (count == 1) {
                        search_discreption.setText(null);
                        count = 0;
                    } else {
                        search_discreption.setText(null);
                    }

                    break;

                case R.id.search_image:
                    FindMenu menu = m.get(position);
                    if (mlistener!= null){
                        mlistener.HotSeacherClick(menu);
                    }

                    break;
                default:
                    break;

            }
        }


    }
}
